
<template>
  <div class="execution">
    <el-dialog
      :title="'预览数据'"
      :visible="!!previewTable"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :destroy-on-close="true"
      width="80%"
    >   
    <Preview
      v-if="previewTable !== null"
      @close="previewTable = null"
      :target="previewTable"
    />
    </el-dialog>
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
      >
        <template
          slot-scope="scope"
          slot="menu"
        >
          <!-- <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="handleView(scope.row, scope.index)"
          >预览</el-button> -->
        </template>
        <template slot="menuRight">
          <el-button
            type="primary" size="small"
            @click="exportClick"
          >导出</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, preview, dataExport } from '@/api/rca/viewcksystemcleanview'
import { tableOption, prefixInteger, startDate, endDate } from '@/const/crud/rca/viewcksystemcleanview'
import { mapGetters } from 'vuex'
import Preview from "./components/Preview";
import { handleExcelResponse } from "@/util/util";

export default {
  name: 'viewcksystemcleanview',
  components: {
    Preview,
  },
  data () {
    return {
      searchForm: {
        tradeDate: [startDate, endDate]
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,

      previewTable: null,
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        export: this.vaildData(this.permissions.rca_swckclearsystemcompare_excel_export, false),
        confirm: this.vaildData(this.permissions.rca_swckclearsystemcompare_confirm, false),
        notice: this.vaildData(this.permissions.rca_swckclearsystemcompare_notice, false)
      };
    }
  },
  methods: {
    getList (page, params) {
      this.tableLoading = true
      const { tradeDate, lineCode } = this.searchForm
      const [startDate, endDate] = tradeDate || []
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm, {
        startDate,
        endDate,
      })).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    async exportClick (row) {
      this.dataExport();
    },
    async dataExport () {
      const { tradeDate = [] } = this.searchForm;
      const [startDate, endDate] = tradeDate;
      if (!startDate || !endDate) {
        this.$message.error("日期范围不能为空");
        return
      }
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await dataExport(Object.assign({}, this.searchForm, { startDate, endDate }));
        // handleExcelResponse(res, `${startDate}至${endDate}导出.xlsx`);
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      const [startDate, endDate] = this.searchForm.tradeDate || [];
      if (!startDate || !endDate) {
        this.$message.error("日期范围不能为空");
        done();
        return;
      }
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },
    handleView (row, index) {
      this.previewTable = row
    },
    searchReset () {

              this.page.currentPage = 1
              this.getList(this.page);
    },
  }
}
</script>
